<?php

namespace  app\service;


use app\model\PartnerLogModel;
use app\model\PartnerModel;
use app\model\PartnerRoleModel;
use app\model\PartnerUserModel;
use app\model\PermissionModel;
use app\model\RoleModel;
use app\utils\Md5Service;
use think\facade\Db;

Class PartnerUserService{

    public static  function getAllPartnerUser($param) :array
    {
        return PartnerUserModel::getPartnerUserByPartnerId($param);
    }

    public static  function getAllPartnerInfoById($param)
    {
         $uinfo=PartnerUserModel::getAllPartnerInfoByUId($param);
           $where=['id'=>$uinfo['role_id']];
        $role=[];
        $res=PartnerRoleModel::find_role($where);
          array_push($role,$res['name']);
        $uinfo['role']=$role;
         $uinfo['permission_ids']=$res['permission_ids'];
        //   if ($res['permission_ids']=='*'){
        //     $uinfo['permission_ids']= Db::table('xl_partner_permission')
        //         ->where("status","=",1)
        //         ->select()->toArray();
        // }else{
        //     $uinfo['permission_ids']= Db::table('xl_partner_permission')->where("id","in",$res['permission_ids'])
        //         ->where("status","=",1)
        //         ->select()->toArray();
        // }
        
        //  $uinfo['permission_ids']= Db::table('xl_partner_permission')->where("id","in",$res['permission_ids'])
        //         ->where("status","=",1)
        //         ->select()->toArray();
        return $uinfo;
    }

    public static  function add_user($param) :int
    {


        $partnerUserInfo=PartnerUserModel::find_user($param['mobile']);
        if (!empty($partnerUserInfo)){
            return  10001;
        }

        $salt=GetRandStr(10);
        $data=[
            'name'=>$param['name'],
            'add_time'=>getNowDateTime(),
            'mobile'=>$param['mobile'],
            'status'=>$param['status'],
            'update_time'=>getNowDateTime(),
            'role_id'=>$param['role_id'],
            'nickname'=>$param['nickname'],
            'password'=>Md5Service::encryptMd5($param['password'],$salt),
            'salt'=>$salt,
            'partner_id'=>$param['partner_id'],
             'api_salt'=>GetRandStr(10),
            'api_salt_app'=>GetRandStr(10),
            'wms_api_salt_h5'=>GetRandStr(10),
            'wms_api_salt_app'=>GetRandStr(10),
        ];
        Db::startTrans();
        $addPartnerId=PartnerUserModel::add_user($data);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'新增合作商用户',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
         if ($addPartnerId){
             Db::commit();
             return 10000;
         }else{
             // 回滚事务
             Db::rollback();
             return 10003;
         }
    }

    public static  function modify_user($param,$id) :int
    {
        //查询mobile是否存在
        $partnerUserInfo=PartnerUserModel::find_user2($param['mobile'],$id);
        if (!empty($partnerUserInfo)){
            return  10001;
        }

        $salt=GetRandStr(10);
        $data=[
            'name'=>$param['name'],
            'add_time'=>getNowDateTime(),
            'mobile'=>$param['mobile'],
            'status'=>$param['status'],
            'update_time'=>getNowDateTime(),
            'role_id'=>$param['role_id'],
            'nickname'=>$param['nickname'],
            'password'=>Md5Service::encryptMd5($param['password'],$salt),
            'salt'=>$salt,
            'partner_id'=>$param['partner_id']
        ];
        Db::startTrans();
        $addPartnerId=PartnerUserModel::modify_user($data,$id);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'修改合作商用户',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPartnerId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static  function del_user($param,$id) :int
    {
        //查询mobile是否存在

        $partnerUserInfo=PartnerUserModel::findUserById($id);
         if (empty($partnerUserInfo)){
            return  10001;
        }
         if ($partnerUserInfo['partner_id']!=$partnerUserInfo['partner_id']){
             return  10002;
         }


        Db::startTrans();
        $addPartnerId=PartnerUserModel::del_user($id);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'删除合作商用户',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPartnerId){
            $redisLink=linkRedis(6);
            $key='partner_user_'.$id;
            $redisLink->del($key);
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public  static  function  platforminfo($partner_id)
    {
        return PartnerModel::platforminfo($partner_id);
    }





}